/**
 * Created by kedong on 2016/5/30.
 */
window.onload = function () {
    /**
     * 商品清单
     */
    var render_goods_list = function () {
        var container = $(".J_goods_list_container"),
            template = $(".J_goods_item_template"),
        // 购物车数据
            allData = YQ.car.getAll(),
        // 商品清单
            orderDetail = allData.orderDetail,
        // 商品名称
            goods_name,
        // 商品数量
            goods_count,
        // 商品价格
            goods_price,
            newItem;

        if (orderDetail && orderDetail.length > 0) {
            for (var i = 0, count = orderDetail.length; i < count; i++) {
                var order = orderDetail[i];
                newItem = template.clone();
                $(newItem).removeClass("J_goods_item_template hidden");

                goods_name = order.name;
                goods_count = order.amount;
                goods_price = order.price;

                $(newItem).find(".J_goods_name").text(goods_name);
                $(newItem).find(".J_goods_count").text(goods_count);
                $(newItem).find(".J_goods_price").text(goods_price);

                $(container).append(newItem);
            }
        }
        // 总计
        $(".J_amount_num").text(allData.totalPrice);
        $(".J_shopping_car_amount").text(allData.totalPrice);
    };
    render_goods_list();

    /**
     * 用户信息
     */
    var user = YQ.get("user");
    $(".J_linckman_mobile").text(user.mobile);
    $(".J_linckman_name").text(user.name);

    /**
     * 取餐时间
     */
    $(".J_mealTime").text(YQ.getWithOutJson("mealTime"));

    /**
     * 提交订单按钮
     */
    $(".J_buy_submit").tap(function () {
        if (YQ.isEnabled(this))
            location.href = "order_info.html"
    });

    /**
     * 弹出地址选择框
     */
    $(".J_address_select_btn").tap(function () {
        $("#Jpopover").show();
    });

    /**
     * 选择地址
     */
    $("#Jpopover").find(".row").tap(function () {
        var address = $(this).attr("data_value"),
            text = $(this).text();
        $(".J_address_detail").text(text).attr("data_value", address);
        if (address) {
            $(".J_buy_submit").removeClass("disabled");
        } else {
            $(".J_buy_submit").addClass("disabled");
        }
        $("#Jpopover").hide();
    });
};
